package iot

import (
	"errors"
	"gorm.io/gorm"
	"iot-base/common/dbobj"
)

type (
	Catalogs dbobj.Catalog

	CatalogMode struct {
		db *gorm.DB
	}
)

func NewCatalogMode(db *gorm.DB) *CatalogMode {
	return &CatalogMode{
		db,
	}
}

//QueryAll 查询所有.
func (c *CatalogMode) QueryAll() ([]Catalogs, error) {
	var list []Catalogs
	err := c.db.Model(&Catalogs{}).Find(&list).Limit(100).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return []Catalogs{}, nil
	}
	if len(list) > 100 {
		return list, err
	}
	return append([]Catalogs{}, list...), err
}
